import { fetchCardListAPI, deleteCardAPI, addCardAPI, editCardAPI, renewCardAPI, findCardAPI, fetchCardDetailAPI } from '@/api/car/monthCardApi'

const actions = {
  /* 发送请求获取月卡列表的action */
  async fetchCardList(context, pageParams) {
    const { data } = await fetchCardListAPI(pageParams)
    context.commit('updateCardList', data)
  },

  /* 发送请求删除月卡的action */
  async deleteCard(context, id) {
    await deleteCardAPI(id)
    return '删除月卡成功'
  },
  /* 发送请求添加月卡的action */
  async addCard(context, data) {
    await addCardAPI(data)
    return '添加月卡成功'
  },
  /* 发送请求编辑月卡的action */
  async editCard(context, data) {
    await editCardAPI(data)
    return '编辑月卡成功'
  },
  /* 发送请求续费月卡的action */
  async renewCard(context, data) {
    await renewCardAPI(data)
    return '续费月卡成功'
  },
  /* 发送请求查找月卡的action */
  async findCard(context, id) {
    const { data } = await findCardAPI(id)
    return data
  },
  /* 发送请求获取月卡详情的action */
  async fetchCardDetail(context, id) {
    const { data } = await fetchCardDetailAPI(id)
    context.commit('updateCardDetail',data)
  }
}
const mutations = {
  /* 更新月卡列表的mutation */
  updateCardList(state, cardListInfo) {
    state.cardListInfo = cardListInfo
  },
  /* 更新月卡详情的mutation */
  updateCardDetail(state,cardDetail){
    state.cardDetail=cardDetail
  }
}
const state = () => {
  return {
    cardListInfo: {},
    cardDetail:{}
  }
}
const getters = {
  cardList: state => state.cardListInfo.rows,
  total: state => state.cardListInfo.total
}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
